#ifndef ROPPENSERVER_H_
#define ROPPENSERVER_H_

#include <QtNetwork>
#include <QObject>
#include "../../include/dto/3d/matrix.h"
#include "../../glwidget.h"

class RoppenServer : public QObject{
	Q_OBJECT
public:
	RoppenServer(DisplayPanel *_panel): QObject() {
		panel = _panel;
		server = new QTcpServer();
		server->listen(QHostAddress::Any,6667);
		connect(server, SIGNAL(newConnection()), this, SLOT(readCommands()));
	}
	~RoppenServer() {

	}
public slots:
void readCommands() {
	clientConnection = server->nextPendingConnection();
	connect(clientConnection,SIGNAL(readyRead()),this,SLOT(read()));
}
void read() {
	char buf[1024];
	qint64 lineLength = clientConnection->readLine(buf, sizeof(buf));
	while (lineLength != -1) {
		QString string = QString::fromUtf8(buf);
		QString objectSelect = string.mid(0,2);
		QString fieldSelect = string.mid(2,2);
		QString data = string.mid(4,string.length()-4);
		int oS = objectSelect.toInt();
		int fS = fieldSelect.toInt();
//		printf("select: %d %d\n",oS,fS);
		int i;
		float f;
		switch (oS) {
		case 10:
			i = data.toInt();
			switch (fS) {
			case 0:
				panel->rot->get()->x->set(i);
				break;
			case 1:
				panel->rot->get()->y->set(i);
				break;
			case 2:
				panel->rot->get()->z->set(i);
				break;
			}
			break;
		case 11:
			f = data.toFloat();
			switch (fS) {
			case 0:
				panel->pos->get()->x->set(f);
				break;
			case 1:
				panel->pos->get()->y->set(f);
				break;
			case 2:
				panel->pos->get()->z->set(f);
				break;
			}
			break;
		case 12:
			switch (fS) {
			case 0:
				i = data.toInt();
				panel->text->get()->length->set(i);
				break;
			case 1:
				i = data.toInt();
				panel->text->get()->stop->set(i);
				break;
			case 2:
				i = data.toInt();
				panel->text->get()->start->set(i);
				break;
			case 3:
				i = data.toInt();
				panel->text->get()->playMode->set(i);
				printf("playMode: %d\n",i);
				break;
			case 4:
				i = data.toInt();
				panel->text->get()->startStop->set(i);
				break;
			case 5:
				i = data.toInt();
				panel->text->get()->fps->set(i);
				break;
			case 6:
				i = data.toInt();
				panel->text->get()->camToTime->set(i);
				break;
			case 7:
				i = data.toInt();
				panel->text->get()->opacity->set(i);
				break;
			case 8:
				f = data.toFloat();
				panel->text->get()->current->set(f);
				break;
			case 9:
				f = data.toFloat();
				panel->text->get()->frameIncrement->set(f);
				break;
			case 10:
				printf("filebase: ");
				data = data.mid(0,data.length()-1);
				data = data.trimmed();
				panel->text->get()->filebase->set(data.toUtf8().data());
				printf(data.toUtf8().data());
				printf("\n");
				break;
			case 11:
				printf("filename: ");
				data = data.mid(0,data.length()-1);
				data = data.trimmed();
				panel->text->get()->filename->set(data.toUtf8().data());
				printf(data.toUtf8().data());
				printf("\n");
				break;
			}
		}
		lineLength = clientConnection->readLine(buf, sizeof(buf));
	}
}
private:
	QTcpServer *server;
	QTcpSocket *clientConnection;
	DisplayPanel *panel;
};

class RoppenTextServer : public QObject{
	Q_OBJECT
public:
	RoppenTextServer(GLWidget *_widget): QObject() {
		widget = _widget;
		server = new QTcpServer();
		server->listen(QHostAddress::Any,6668);
		connect(server, SIGNAL(newConnection()), this, SLOT(readCommands()));
	}
	~RoppenTextServer() {

	}
public slots:
void readCommands() {
	clientConnection = server->nextPendingConnection();
	connect(clientConnection,SIGNAL(readyRead()),this,SLOT(read()));
}
void read() {
	char buf[1024];
	qint64 lineLength = clientConnection->readLine(buf, sizeof(buf));
	while (lineLength != -1) {
		QString string = QString::fromUtf8(buf);
		widget->setText(string);
		lineLength = clientConnection->readLine(buf, sizeof(buf));
	}
}
private:
	QTcpServer *server;
	QTcpSocket *clientConnection;
	GLWidget *widget;
};

class PingServer : public QObject{
	Q_OBJECT
public:
	PingServer(): QObject() {
		server = new QTcpServer();
		server->listen(QHostAddress::Any,6669);
		socket = new QTcpSocket(this);
		connect(server, SIGNAL(newConnection()), this, SLOT(readCommands()));
	}
	~PingServer() {

	}
public slots:
void readCommands() {
	clientConnection = server->nextPendingConnection();
	connect(clientConnection,SIGNAL(readyRead()),this,SLOT(read()));
}
void read() {
	char buf[1024];
	qint64 lineLength = clientConnection->readLine(buf, sizeof(buf));
	while (lineLength != -1) {
		QString string = QString::fromUtf8(buf);
		if (socket->state()!= QAbstractSocket::ConnectedState) {
			socket ->abort();
			socket->connectToHost("127.0.0.1",6671);
		}
		if (socket->state()== QAbstractSocket::ConnectedState) {
			socket->write(string.toUtf8().data());
			socket->write("\n");
			printf(string.toUtf8().data());
			printf("\n");
			socket->flush();
			socket->disconnect();
			socket->close();
		}
		lineLength = clientConnection->readLine(buf, sizeof(buf));
	}
}
private:
	QTcpServer *server;
	QTcpSocket *clientConnection;
	QTcpSocket *socket;

};
#endif /*ROPPENSERVER_H_*/
